home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / TUTOROOT.PAK / STEP05.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  3KB  |  134 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1994 by Borland International
  3. //   Tutorial application -- step05.cpp
  4. //----------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #include <owl/applicat.h>
  7. #include <owl/framewin.h>
  8. #include <owl/dc.h>
  9. #include <owl/inputdia.h>
  10. #include <stdlib.h>
  11.  
  12. class TDrawWindow : public TWindow {
  13.   public:
  14.     TDrawWindow(TWindow* parent = 0);
  15.    ~TDrawWindow()
  16.     {
  17.       delete DragDC;
  18.       delete Pen;
  19.     }
  20.  
  21.     void SetPenSize(int newSize);
  22.  
  23.   protected:
  24.     TDC* DragDC;
  25.     int PenSize;
  26.     TPen* Pen;
  27.  
  28.     // Override member function of TWindow
  29.     bool CanClose();
  30.  
  31.     // Message response functions
  32.     void EvLButtonDown(uint, TPoint&);
  33.     void EvRButtonDown(uint, TPoint&);
  34.     void EvMouseMove(uint, TPoint&);
  35.     void EvLButtonUp(uint, TPoint&);
  36.  
  37.   DECLARE_RESPONSE_TABLE(TDrawWindow);
  38. };
  39.  
  40. DEFINE_RESPONSE_TABLE1(TDrawWindow, TWindow)
  41.   EV_WM_LBUTTONDOWN,
  42.   EV_WM_RBUTTONDOWN,
  43.   EV_WM_MOUSEMOVE,
  44.   EV_WM_LBUTTONUP,
  45. END_RESPONSE_TABLE;
  46.  
  47. TDrawWindow::TDrawWindow(TWindow* parent)
  48. {
  49.   Init(parent, 0, 0);
  50.   DragDC  = 0;
  51.   PenSize = 1;
  52.   Pen     = new TPen(TColor::Black, PenSize);
  53. }
  54.  
  55. bool
  56. TDrawWindow::CanClose()
  57. {
  58.   return MessageBox("Do you want to save?", "Drawing has changed",
  59.                     MB_YESNO | MB_ICONQUESTION) == IDNO;
  60. }
  61.  
  62. void
  63. TDrawWindow::EvLButtonDown(uint, TPoint& point)
  64. {
  65.   Invalidate();
  66.  
  67.   if (!DragDC) {
  68.     SetCapture();
  69.     DragDC = new TClientDC(*this);
  70.     DragDC->SelectObject(*Pen);
  71.     DragDC->MoveTo(point);
  72.   }
  73. }
  74.  
  75. void
  76. TDrawWindow::EvRButtonDown(uint, TPoint&)
  77. {
  78.   char inputText[6];
  79.  
  80.   wsprintf(inputText, "%d", PenSize);
  81.   if ((TInputDialog(this, "Line Thickness",
  82.                         "Input a new thickness:",
  83.                         inputText,
  84.                         sizeof(inputText))).Execute() == IDOK) {
  85.     int newPenSize = atoi(inputText);
  86.  
  87.     if (newPenSize < 0)
  88.       newPenSize = 1;
  89.  
  90.     SetPenSize(newPenSize);
  91.   }
  92. }
  93.  
  94. void
  95. TDrawWindow::EvMouseMove(uint, TPoint& point)
  96. {
  97.   if (DragDC)
  98.     DragDC->LineTo(point);
  99. }
  100.  
  101. void
  102. TDrawWindow::EvLButtonUp(uint, TPoint&)
  103. {
  104.   if (DragDC) {
  105.     ReleaseCapture();
  106.     delete DragDC;
  107.     DragDC = 0;
  108.   }
  109. }
  110.  
  111. void
  112. TDrawWindow::SetPenSize(int newSize)
  113. {
  114.   delete Pen;
  115.   PenSize = newSize;
  116.   Pen     = new TPen(TColor::Black, PenSize);
  117. }
  118.  
  119. class TDrawApp : public TApplication {
  120.   public:
  121.     TDrawApp() : TApplication() {}
  122.  
  123.     void InitMainWindow()
  124.     {
  125.       SetMainWindow(new TFrameWindow(0, "Drawing Pad", new TDrawWindow));
  126.     }
  127. };
  128.  
  129. int
  130. OwlMain(int /*argc*/, char* /*argv*/ [])
  131. {
  132.   return TDrawApp().Run();
  133. }
  134.